﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
    /// <summary>
    /// dao_api_delay 的摘要说明
    /// </summary>
    public class dao_api_delay
    {
        public dao_api_delay()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperOleDb.GetMaxID("id", "api_delay");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from api_delay");
            strSql.Append(" where id=@id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
            parameters[0].Value = id;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Maticsoft.Model.model_api_delay model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into api_delay(");
            strSql.Append("apiname,apiurl,apimemo,status,contenttype,content,isusing,httpmethod,appcode,appqueue)");
            strSql.Append(" values (");
            strSql.Append("@apiname,@apiurl,@apimemo,@status,@contenttype,@content,@isusing,@httpmethod,@appcode,@appqueue)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@apiname", OleDbType.VarChar,255),
					new OleDbParameter("@apiurl", OleDbType.VarChar,255),
					new OleDbParameter("@apimemo", OleDbType.VarChar,255),
					new OleDbParameter("@status", OleDbType.VarChar,255),
					new OleDbParameter("@contenttype", OleDbType.VarChar,255),
					new OleDbParameter("@content", OleDbType.VarChar,255),
                    new OleDbParameter("@isusing", OleDbType.Boolean),  
                    new OleDbParameter("@httpmethod", OleDbType.VarChar,255),
                    new OleDbParameter("@appcode", OleDbType.VarChar,255),  
                    new OleDbParameter("@appqueue", OleDbType.VarChar,255)
                                          };


            parameters[0].Value = model.apiname;
            parameters[1].Value = model.apiurl;
            parameters[2].Value = model.apimemo;
            parameters[3].Value = model.status;
            parameters[4].Value = model.contenttype;
            parameters[5].Value = model.content;
            parameters[6].Value = model.isusing;
            parameters[7].Value = model.httpmethod;
            parameters[8].Value = model.appcode;
            parameters[9].Value = model.appqueue;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.model_api_delay model)
        {   
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update api_delay set ");
            strSql.Append("apiname=@apiname,");
            strSql.Append("apiurl=@apiurl,");
            strSql.Append("apimemo=@apimemo,");
            strSql.Append("status=@status,");
            strSql.Append("contenttype=@contenttype,");
            strSql.Append("content=@content,");
            strSql.Append("isusing=@isusing,");
            strSql.Append("httpmethod=@httpmethod,");
            strSql.Append("appcode=@appcode,");
            strSql.Append("appqueue=@appqueue");

            strSql.Append(" where id=@id");

            OleDbParameter[] parameters = {
					new OleDbParameter("@apiname", OleDbType.VarChar,255),
					new OleDbParameter("@apiurl", OleDbType.VarChar,255),
					new OleDbParameter("@apimemo", OleDbType.VarChar,255),
					new OleDbParameter("@status", OleDbType.VarChar,255),
					new OleDbParameter("@contenttype", OleDbType.VarChar,255),
					new OleDbParameter("@content", OleDbType.VarChar,255),
                    new OleDbParameter("@isusing", OleDbType.Boolean),  
                    new OleDbParameter("@httpmethod", OleDbType.VarChar,255),
                    new OleDbParameter("@appcode", OleDbType.VarChar,255),  
                    new OleDbParameter("@appqueue", OleDbType.VarChar,255),
					new OleDbParameter("@id", OleDbType.Integer,4)};

            parameters[0].Value = model.apiname;
            parameters[1].Value = model.apiurl;
            parameters[2].Value = model.apimemo;
            parameters[3].Value = model.status;
            parameters[4].Value = model.contenttype;
            parameters[5].Value = model.content;
            parameters[6].Value = model.isusing;
            parameters[7].Value = model.httpmethod;
            parameters[8].Value = model.appcode;
            parameters[9].Value = model.appqueue;
            parameters[10].Value = model.id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from api_delay ");
            strSql.Append(" where id=@id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from api_delay ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.model_api_delay GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,apiname,apiurl,apimemo,status,contenttype,content,isusing,httpmethod,appcode,appqueue from api_delay ");
            strSql.Append(" where id=@id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
            parameters[0].Value = id;

            Maticsoft.Model.model_api_delay model = new Maticsoft.Model.model_api_delay();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["apiname"] != null && ds.Tables[0].Rows[0]["apiname"].ToString() != "")
                {
                    model.apiname = ds.Tables[0].Rows[0]["apiname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["apiurl"] != null && ds.Tables[0].Rows[0]["apiurl"].ToString() != "")
                {
                    model.apiurl = ds.Tables[0].Rows[0]["apiurl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["apimemo"] != null && ds.Tables[0].Rows[0]["apimemo"].ToString() != "")
                {
                    model.apimemo = ds.Tables[0].Rows[0]["apimemo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["status"] != null && ds.Tables[0].Rows[0]["status"].ToString() != "")
                {
                    model.status = ds.Tables[0].Rows[0]["status"].ToString();
                }
                if (ds.Tables[0].Rows[0]["contenttype"] != null && ds.Tables[0].Rows[0]["contenttype"].ToString() != "")
                {
                    model.contenttype = ds.Tables[0].Rows[0]["contenttype"].ToString();
                }
                if (ds.Tables[0].Rows[0]["content"] != null && ds.Tables[0].Rows[0]["content"].ToString() != "")
                {
                    model.content = ds.Tables[0].Rows[0]["content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["isusing"] != null && ds.Tables[0].Rows[0]["isusing"].ToString() != "")
                {
                    model.isusing = bool.Parse(ds.Tables[0].Rows[0]["isusing"].ToString());
                }
                if (ds.Tables[0].Rows[0]["httpmethod"] != null && ds.Tables[0].Rows[0]["httpmethod"].ToString() != "")
                {
                    model.httpmethod = ds.Tables[0].Rows[0]["httpmethod"].ToString();
                }
                
                if (ds.Tables[0].Rows[0]["appcode"] != null && ds.Tables[0].Rows[0]["appcode"].ToString() != "")
                {
                    model.appcode = ds.Tables[0].Rows[0]["appcode"].ToString();
                }

                if (ds.Tables[0].Rows[0]["appqueue"] != null && ds.Tables[0].Rows[0]["appqueue"].ToString() != "")
                {
                    model.appqueue = ds.Tables[0].Rows[0]["appqueue"].ToString();
                }

                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.model_api_delay GetModelByAppCode(string appcode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,apiname,apiurl,apimemo,status,contenttype,content,isusing,httpmethod,appcode,appqueue from api_delay ");
            strSql.Append(" where appcode=@appcode");
            OleDbParameter[] parameters = {
					new OleDbParameter("@appcode", OleDbType.VarChar,255)
			};

            parameters[0].Value = appcode;

            Maticsoft.Model.model_api_delay model = new Maticsoft.Model.model_api_delay();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["apiname"] != null && ds.Tables[0].Rows[0]["apiname"].ToString() != "")
                {
                    model.apiname = ds.Tables[0].Rows[0]["apiname"].ToString();
                }
                if (ds.Tables[0].Rows[0]["apiurl"] != null && ds.Tables[0].Rows[0]["apiurl"].ToString() != "")
                {
                    model.apiurl = ds.Tables[0].Rows[0]["apiurl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["apimemo"] != null && ds.Tables[0].Rows[0]["apimemo"].ToString() != "")
                {
                    model.apimemo = ds.Tables[0].Rows[0]["apimemo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["status"] != null && ds.Tables[0].Rows[0]["status"].ToString() != "")
                {
                    model.status = ds.Tables[0].Rows[0]["status"].ToString();
                }
                if (ds.Tables[0].Rows[0]["contenttype"] != null && ds.Tables[0].Rows[0]["contenttype"].ToString() != "")
                {
                    model.contenttype = ds.Tables[0].Rows[0]["contenttype"].ToString();
                }
                if (ds.Tables[0].Rows[0]["content"] != null && ds.Tables[0].Rows[0]["content"].ToString() != "")
                {
                    model.content = ds.Tables[0].Rows[0]["content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["isusing"] != null && ds.Tables[0].Rows[0]["isusing"].ToString() != "")
                {
                    model.isusing = bool.Parse(ds.Tables[0].Rows[0]["isusing"].ToString());
                }
                if (ds.Tables[0].Rows[0]["httpmethod"] != null && ds.Tables[0].Rows[0]["httpmethod"].ToString() != "")
                {
                    model.httpmethod = ds.Tables[0].Rows[0]["httpmethod"].ToString();
                }

                if (ds.Tables[0].Rows[0]["appcode"] != null && ds.Tables[0].Rows[0]["appcode"].ToString() != "")
                {
                    model.appcode = ds.Tables[0].Rows[0]["appcode"].ToString();
                }

                if (ds.Tables[0].Rows[0]["appqueue"] != null && ds.Tables[0].Rows[0]["appqueue"].ToString() != "")
                {
                    model.appqueue = ds.Tables[0].Rows[0]["appqueue"].ToString();
                }

                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,apiname,apiurl,apimemo,status,contenttype,content,isusing,httpmethod,appcode,appqueue ");
            strSql.Append(" FROM api_delay ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            else
            {
                strSql.Append(" order by id DESC");
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM api_delay ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from api_delay T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOleDb.Query(strSql.ToString());
        }



        #endregion  Method


    }
}